package com.stp.test;

import com.stp.constant.annotation.AsyncExec;
import com.stp.constant.enums.AsyncTypeEnum;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

@Slf4j
public class MyInvocationHandler implements InvocationHandler {

    private Object proxy;

    public MyInvocationHandler(Object proxy) {
        this.proxy = proxy;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        log.info("执行方法前");
        Object invoke = method.invoke(this.proxy, args);
        log.info("执行方法后");
        return invoke;
    }

    /**
     * loader – 用于定义代理类的类加载器
     * interfaces – 代理类要实现的接口列表
     * h – 将方法调用分派给的调用处理程序
     */
    public Object getProxy() {
        return Proxy.newProxyInstance(this.getClass().getClassLoader(), proxy.getClass().getInterfaces(), this);
    }
}
